﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Amf4Asmx.IO
{
    public class BigEndianBinaryReader : BinaryReader
    {
        public BigEndianBinaryReader(Stream input) : base(input)
        {
        }

        public BigEndianBinaryReader(Stream input, Encoding encoding) 
            : base(input, encoding)
        {
        }

        public override double ReadDouble()
        {
            return BitConverter.ToDouble(Reverse(ReadBytes(8)), 0);
        }

        public override decimal ReadDecimal()
        {
            throw new NotImplementedException();
        }

        public override short ReadInt16()
        {
            return BitConverter.ToInt16(Reverse(ReadBytes(2)), 0);
        }

        public override int ReadInt32()
        {
            return BitConverter.ToInt32(Reverse(ReadBytes(4)), 0);
        }

        public override long ReadInt64()
        {
            return BitConverter.ToInt64(Reverse(ReadBytes(8)), 0);
        }

        public override float ReadSingle()
        {
            return BitConverter.ToSingle(Reverse(ReadBytes(4)), 0);
        }

        public override ushort ReadUInt16()
        {
            return BitConverter.ToUInt16(Reverse(ReadBytes(2)), 0);
        }

        public override uint ReadUInt32()
        {
            return BitConverter.ToUInt32(Reverse(ReadBytes(4)), 0);
        }

        public override ulong ReadUInt64()
        {
            return BitConverter.ToUInt64(Reverse(ReadBytes(8)), 0);
        }

        private byte[] Reverse(byte[] p)
        {
            Array.Reverse(p);
            return p;
        }

    }
}
